<template>
  <div class="add-member">
    <!-- 页面头部 -->
    <div class="page-header">
      <div class="header-content">
        <div class="header-back" @click="goBack">
          <svg viewBox="0 0 24 24" width="20" height="20" fill="currentColor">
            <path d="M15.41 7.41L14 6l-6 6 6 6 1.41-1.41L10.83 12l4.59-4.59z"/>
          </svg>
        </div>
        <div>
          <h2>添加社区成员</h2>
          <p class="header-subtitle">为社区添加新的管理成员</p>
        </div>
      </div>
    </div>

    <!-- 主要内容区域 -->
    <div class="main-content">
      <!-- 成员信息卡片 -->
      <div class="member-card">
        <div class="card-header">
          <div class="header-icon">
            <svg viewBox="0 0 24 24" width="20" height="20" fill="currentColor">
              <path d="M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z"/>
            </svg>
          </div>
          <h3>成员信息</h3>
        </div>

        <form @submit.prevent="handleSubmit" class="member-form">
          <!-- 选择社区 -->
          <div class="form-item">
            <label for="community">选择社区 *</label>
            <div class="input-group">
              <div class="input-icon">
                <svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor">
                  <path d="M12 5.69l5 4.5V18h-2v-6H9v6H7v-7.81l5-4.5M12 3L2 12h3v8h6v-6h2v6h6v-8h3L12 3z"/>
                </svg>
              </div>
              <select 
                id="community"
                v-model="formData.communityId" 
                required
                :class="{ 'error': errors.communityId }"
              >
                <option value="">请选择社区</option>
                <option v-for="item in communityList" 
                        :key="item.communityId" 
                        :value="item.communityId">
                  {{ item.name }}
                </option>
              </select>
            </div>
            <p v-if="errors.communityId" class="error-message">{{ errors.communityId }}</p>
          </div>

          <!-- 用户名和密码 -->
          <div class="form-row">
            <div class="form-item">
              <label for="username">用户名 *</label>
              <div class="input-group">
                <div class="input-icon">
                  <svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor">
                    <path d="M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z"/>
                  </svg>
                </div>
                <input 
                  id="username"
                  v-model="formData.username" 
                  type="text" 
                  required 
                  placeholder="请输入用户名"
                  :class="{ 'error': errors.username }"
                />
              </div>
              <p v-if="errors.username" class="error-message">{{ errors.username }}</p>
            </div>
            
            <div class="form-item">
              <label for="password">密码 *</label>
              <div class="input-group">
                <div class="input-icon">
                  <svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor">
                    <path d="M18 8h-1V6c0-2.76-2.24-5-5-5S7 3.24 7 6v2H6c-1.1 0-2 .9-2 2v10c0 1.1.9 2 2 2h12c1.1 0 2-.9 2-2V10c0-1.1-.9-2-2-2zm-6 9c-1.1 0-2-.9-2-2s.9-2 2-2 2 .9 2 2-.9 2-2 2zm3.1-9H8.9V6c0-1.71 1.39-3.1 3.1-3.1 1.71 0 3.1 1.39 3.1 3.1v2z"/>
                  </svg>
                </div>
                <input 
                  id="password"
                  v-model="formData.password" 
                  type="password" 
                  required 
                  placeholder="请输入密码"
                  :class="{ 'error': errors.password }"
                />
              </div>
              <p v-if="errors.password" class="error-message">{{ errors.password }}</p>
            </div>
          </div>

          <!-- 真实姓名和联系电话 -->
          <div class="form-row">
            <div class="form-item">
              <label for="realName">真实姓名 *</label>
              <div class="input-group">
                <div class="input-icon">
                  <svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor">
                    <path d="M9 11H7v6h2v-6zm4 0h-2v6h2v-6zm4 0h-2v6h2v-6zm2.5-9H19v4h-4V2h-1.5v4h-4V2H8v4H4v16h4v-2h2v2h4v-2h2v2h4v-2h2v2h4V6h-4V2zm-13 9h2v-2h-2v2zm4 0h2v-2h-2v2zm4 0h2v-2h-2v2z"/>
                  </svg>
                </div>
                <input 
                  id="realName"
                  v-model="formData.realName" 
                  type="text" 
                  required 
                  placeholder="请输入真实姓名"
                  :class="{ 'error': errors.realName }"
                />
              </div>
              <p v-if="errors.realName" class="error-message">{{ errors.realName }}</p>
            </div>
            
            <div class="form-item">
              <label for="phone">联系电话 *</label>
              <div class="input-group">
                <div class="input-icon">
                  <svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor">
                    <path d="M6.62 10.79c1.44 2.83 3.76 5.14 6.59 6.59l2.2-2.2c.27-.27.67-.36 1.02-.24 1.12.37 2.33.57 3.57.57.55 0 1 .45 1 1V20c0 .55-.45 1-1 1-9.39 0-17-7.61-17-17 0-.55.45-1 1-1h3.5c.55 0 1 .45 1 1 0 1.25.2 2.45.57 3.57.11.35.03.74-.25 1.02l-2.2 2.2z"/>
                  </svg>
                </div>
                <input 
                  id="phone"
                  v-model="formData.phone" 
                  type="tel" 
                  required 
                  placeholder="请输入联系电话"
                  :class="{ 'error': errors.phone }"
                />
              </div>
              <p v-if="errors.phone" class="error-message">{{ errors.phone }}</p>
            </div>
          </div>

          <!-- 操作按钮 -->
          <div class="form-actions">
            <button type="button" class="cancel-btn" @click="goBack">
              <svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor">
                <path d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z"/>
              </svg>
              取消
            </button>
            <button type="submit" class="submit-btn">
              <svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor">
                <path d="M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z"/>
              </svg>
              确认添加
            </button>
          </div>
        </form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import request from '../../utils/request'

const router = useRouter()
const communityList = ref([])
const errors = ref({})

const formData = ref({
  username: '',
  password: '',
  realName: '',
  phone: '',
  communityId: '',
  roleType: 'community'
})

// 获取社区列表
const getCommunityList = async () => {
  try {
    const res = await request.get('/admin/getCommunityList')
    communityList.value = res.data
  } catch (error) {
    console.error('获取社区列表失败:', error)
  }
}

// 表单验证
const validateForm = () => {
  const newErrors = {}
  
  if (!formData.value.communityId) {
    newErrors.communityId = '请选择社区'
  }
  
  if (!formData.value.username) {
    newErrors.username = '请输入用户名'
  } else if (formData.value.username.length < 3) {
    newErrors.username = '用户名至少3个字符'
  }
  
  if (!formData.value.password) {
    newErrors.password = '请输入密码'
  } else if (formData.value.password.length < 6) {
    newErrors.password = '密码至少6个字符'
  }
  
  if (!formData.value.realName) {
    newErrors.realName = '请输入真实姓名'
  }
  
  if (!formData.value.phone) {
    newErrors.phone = '请输入联系电话'
  } else if (!/^1[3-9]\d{9}$/.test(formData.value.phone)) {
    newErrors.phone = '请输入正确的手机号码'
  }
  
  errors.value = newErrors
  return Object.keys(newErrors).length === 0
}

// 提交表单
const handleSubmit = async () => {
  if (!validateForm()) {
    return
  }
  
  try {
    await request.post('/admin/addCommunityUser', formData.value)
    alert('添加成功')
    router.push('/admin/community-members')
  } catch (error) {
    console.error('添加失败:', error)
    alert(error.message || '添加失败')
  }
}

// 返回上一页
const goBack = () => {
  router.back()
}

onMounted(() => {
  getCommunityList()
})
</script>

<style scoped>
.add-member {
  padding: 24px;
  background: linear-gradient(135deg, #f0f7ff 0%, #e6f2ff 100%);
  min-height: 100vh;
}

.page-header {
  margin-bottom: 24px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.08);
  padding: 20px;
}

.header-content {
  display: flex;
  align-items: center;
  gap: 16px;
}

.header-back {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: #f1f3f4;
  cursor: pointer;
  transition: all 0.3s ease;
}

.header-back:hover {
  background: #e3f2fd;
  transform: translateX(-2px);
}

.header-back svg {
  color: #5f6368;
}

.page-header h2 {
  font-size: 24px;
  color: #2c3e50;
  margin: 0 0 4px 0;
}

.header-subtitle {
  margin: 0;
  color: #7f8c8d;
  font-size: 14px;
}

.main-content {
  max-width: 1800px;
  margin: 0 auto;
}

.member-card {
  background: white;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.08);
  overflow: hidden;
  margin: 0 auto;
  max-width: 1800px;
}

.card-header {
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
  color: white;
  padding: 20px 24px;
  display: flex;
  align-items: center;
  gap: 12px;
}

.header-icon {
  width: 20px;
  height: 20px;
}

.card-header h3 {
  margin: 0;
  font-size: 18px;
  font-weight: 500;
}

.member-form {
  padding: 30px;
}

.form-item {
  margin-bottom: 24px;
}

.form-item label {
  display: block;
  margin-bottom: 8px;
  color: #2c3e50;
  font-size: 14px;
  font-weight: 500;
}

.input-group {
  position: relative;
  display: flex;
  align-items: center;
}

.input-icon {
  width: 20px;
  height: 20px;
  margin-right: 12px;
  color: #9ca3af;
}

.input-group select,
.input-group input {
  flex: 1;
  padding: 12px 16px;
  border: 1px solid #d1d5db;
  border-radius: 8px;
  font-size: 14px;
  transition: all 0.3s;
  background: #fff;
}

.input-group select:focus,
.input-group input:focus {
  border-color: #4facfe;
  outline: none;
  box-shadow: 0 0 0 3px rgba(79, 172, 254, 0.1);
}

.input-group select.error,
.input-group input.error {
  border-color: #f87171;
}

.form-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 24px;
  margin-bottom: 24px;
}

.error-message {
  margin: 6px 0 0;
  color: #f87171;
  font-size: 12px;
}

.form-actions {
  display: flex;
  justify-content: flex-end;
  gap: 16px;
  margin-top: 32px;
  padding-top: 24px;
  border-top: 1px solid #eee;
}

.form-actions button {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 24px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.3s;
}

.cancel-btn {
  border: 1px solid #d1d5db;
  background: #fff;
  color: #4b5563;
}

.cancel-btn:hover {
  background: #f9fafb;
  border-color: #9ca3af;
}

.submit-btn {
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
  color: white;
  border: none;
  box-shadow: 0 2px 6px rgba(79, 172, 254, 0.3);
}

.submit-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(79, 172, 254, 0.4);
}

@media (max-width: 768px) {
  .add-member {
    padding: 16px;
  }
  
  .main-content {
    max-width: 100%;
  }
  
  .member-card {
    max-width: 100%;
  }
  
  .form-row {
    grid-template-columns: 1fr;
  }
  
  .form-actions {
    flex-direction: column;
  }
  
  .form-actions button {
    width: 100%;
    justify-content: center;
  }
}
</style>